package com.beifu.manager.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.beifu.manager.model.AssetScrap;
import com.beifu.manager.model.AssetScrapDetailExample;
import com.beifu.manager.model.AssetScrapExample;
import com.beifu.manager.model.AssetScrapExample.Criteria;
import com.beifu.manager.service.assetscrap.AssetScrapService;
import com.mds.manager.utils.PageUtils;
import com.mds.manager.utils.ResponseHelp;
import com.utils.JsonUtil;
import com.utils.LayuiPageUtils;

@Controller
@RequestMapping("/assetscrap")
public class AssetScrapController {
	private Logger Log = Logger.getLogger(this.getClass());
	
	@Autowired
	private AssetScrapService assetScrapService;
	
	@RequestMapping("/getAssetScrapIndex")
	public String getAssetScrapIndex(){
		Log.info("++++进入报废单列表页+++++");
		return "/assetscrap/assetscrapindex";
	}
	
	@ResponseBody
	@RequestMapping(value="/list",produces=MediaType.APPLICATION_JSON_VALUE+";charset=utf-8")
	public String getAssetScrapList(HttpServletRequest request){
		String page = request.getParameter("pageNumber");
		String id = request.getParameter("id");
		String status = request.getParameter("status");
		
		AssetScrapExample example = new AssetScrapExample();
		Criteria criteria = example.createCriteria();
		if(!StringUtils.isEmpty(id)){
			criteria.andIdEqualTo(Integer.parseInt(id));
		}
		if(!StringUtils.isEmpty(status)){
			criteria.andStatusEqualTo(status);
		}
		PageUtils Page = new PageUtils(page);
		Page.setQueryParames(example);
		
		Page = assetScrapService.getAssetScrapList(Page);
		return ResponseHelp.responseText(Page);
	}
/**
 * 查看报废单详情	
 * @param request
 * @return
 */
	@RequestMapping("/getAssetScrapDetail")
	public String getAssetScrapDetail(HttpServletRequest request){
		String id = request.getParameter("id");
		String rowJson = request.getParameter("rowJson");//获取表格的第几行
		
		AssetScrap assetScrap = assetScrapService.getAssetScrapDetail(id);
		request.setAttribute("assetScrap", assetScrap);
		request.setAttribute("parent_row", rowJson);
		return "/assetscrap/assetscrapdetail";
	}
	/**
	 * 查看报废单详情中的列表	
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/getScrapDetailTable", produces=MediaType.APPLICATION_JSON_VALUE+";charset=utf-8")
	public String getScrapDetailTable(HttpServletRequest request){
		String id = request.getParameter("id");
		String page = request.getParameter("page");
		
		AssetScrapDetailExample example = new AssetScrapDetailExample();
		
		if(!StringUtils.isEmpty(id)){
			example.createCriteria().andAssetScrapIdEqualTo(Integer.parseInt(id));
		}
		PageUtils Page = new PageUtils(page);
		Page.setQueryParames(example);
		
		Page = assetScrapService.getScrapDetailTable(Page);
		
		LayuiPageUtils layuiPageUtils = new LayuiPageUtils();
		layuiPageUtils.setCode("0");
		layuiPageUtils.setMsg("");
		layuiPageUtils.setCount(Page.getCountNum());
		layuiPageUtils.setData(Page.getList());
		Log.info(ResponseHelp.responseText(layuiPageUtils));
		return ResponseHelp.responseText(layuiPageUtils);
	}
	
	/**
	 * 获取审核列表
	 * @param request
	 * @return
	 */
	@RequestMapping("/getAssetScrapApprove")
	public String getAssetScrapApprove(HttpServletRequest request){
		String id = request.getParameter("id");
		String rowJson = request.getParameter("rowJson");//获取表格的第几行
		
		AssetScrap assetScrap = assetScrapService.getAssetScrapDetail(id);
		request.setAttribute("assetScrap", assetScrap);
		request.setAttribute("parent_row", rowJson);
		return "/assetscrap/assetscrapapprove";
	}
	/**
	 * 报废单通过审核
	 * @param request
	 * @return
	 * @throws IOException 
	 */
	@RequestMapping("/approveAssetsScrap")
	public String approveAssetsScrap(HttpServletRequest request, HttpServletResponse response) throws IOException{
		String id = request.getParameter("id");
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			map = assetScrapService.approveAssetsScrap(id);
		} catch (Exception e) {
			map.put("code", "error");
			map.put("msg", "审核失败");
			Log.error("审核失败", e);
		}
//		request.setAttribute("assetScrap", assetScrap);
		response.setHeader("Charset", "UTF8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/json");
		PrintWriter writer = response.getWriter();
		
		
		// 将map转换为json。
		String json = JsonUtil.toJson(map);
		writer.write(json);
		writer.flush();
		writer.close();
		return null;
	}
	/**
	 * 报废单驳回 窗口展示
	 * @param request
	 * @return
	 */
	@RequestMapping("/showRefuseWindow")
	public String showRefuseWindow(HttpServletRequest request){
		String id = request.getParameter("id");
		Log.info("++++进入报废单列表页+++++");
		request.setAttribute("id", id);
		
		return "/assetscrap/refuse";
	}
	/**
	 * 报废单驳回
	 * @param request
	 * @return
	 * @throws IOException 
	 */
	@RequestMapping(value="/refuseAssetsScrap", produces=MediaType.APPLICATION_JSON_VALUE+";charset=utf-8")
	public String refuseAssetsScrap(HttpServletRequest request, HttpServletResponse response) throws IOException{
		String id = request.getParameter("assetId");
		//添加驳回原因
		String refuseReason = request.getParameter("refuseReason");
		byte[] bytes = refuseReason.getBytes("ISO-8859-1"); //以"ISO-8859-1"方式解析ParameterName字符串
		refuseReason= new String(bytes, "UTF-8"); //再用"utf-8"格式表示ParameterName
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			map = assetScrapService.refuseAssetsScrap(id, refuseReason);
		} catch (Exception e) {
			map.put("code", "error");
			map.put("msg", "审核失败");
			Log.error("审核失败", e);
		}
		response.setHeader("Charset", "UTF8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/json");
		PrintWriter writer = response.getWriter();
		
		
		// 将map转换为json。
		String json = JsonUtil.toJson(map);
		writer.write(json);
		writer.flush();
		writer.close();
		return null;
	}
}
